package cn.sunxyz.common.site.controller;

import cn.sunxyz.common.site.Result;
import cn.sunxyz.common.site.domain.BaseEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * Created by sunxyz on 2017/3/2.
 */
public abstract class BaseController<D extends JpaRepository<T, Long>, T extends BaseEntity> extends DefaultController<D, T, Long> {

    @PostMapping
    @Override
    public Result<String> add(T t) {
        return super.add(t);
    }

    @DeleteMapping("/{id}")
    @Override
    public Result<String> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    public abstract Result<String> update(T t, T news);

    @GetMapping
    public Result<Page<T>> getAll(Pageable pageable) {
        return Result.ok(repository.findAll(pageable));
    }

    @GetMapping("/{id}")
    public Result<T> get(@PathVariable("id") T t) {
        return Result.ok(t);
    }

}
